package com.ruoyi.consumer.aop.aspect;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.consumer.aop.annotation.RecordOpeate;
import com.ruoyi.consumer.aop.convert.RecordService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Component
@Aspect
public class OperateAspect {

    @Autowired
    private RecordService recordService;

    //定义切点
    @Pointcut("@annotation(com.ruoyi.consumer.aop.annotation.RecordOpeate)")
    public void operategetMinOrder() {
    }

    private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            5, 20, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<>(100)
    );

    //定义横向逻辑
    @Around("operategetMinOrder()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 获取注解
        RecordOpeate annotation = getRecordOpeateAnnotation(proceedingJoinPoint);

        // 执行原始方法
        Object result = proceedingJoinPoint.proceed();

        //异步执行
        threadPoolExecutor.execute(() -> {
            //获取方法,获取注解
            try {
                processAsyncLogic(result, annotation, proceedingJoinPoint);

            } catch (Exception e) {
                e.printStackTrace();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }

        });
        return result; //返回结果
    }

    private void processAsyncLogic(Object result, RecordOpeate annotation, ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 异步逻辑使用获取到的 result 和 annotation
        String desc = annotation.desc();

        //获取方法返回值
        System.out.println(JSON.toJSONString(proceedingJoinPoint.getArgs()));
        if (desc.equals("个人下单") || desc.equals("公司下单")) {
            String returnValue = (String) result;
            recordService.takeOrder((Integer) proceedingJoinPoint.getArgs()[0],
                    (Integer) proceedingJoinPoint.getArgs()[1],
                    (Long) proceedingJoinPoint.getArgs()[2],
                    (String) proceedingJoinPoint.getArgs()[3],
                    Long.valueOf(returnValue),
                    desc);
        } else if (desc.equals("个人钱包小订单加注") || desc.equals("公司钱包小订单加注")) {
            recordService.getMinOrder((Long) proceedingJoinPoint.getArgs()[0], desc);
        } else if (desc.equals("微信下单")) {
            recordService.getWxOrder(proceedingJoinPoint.getArgs()[0].toString(), desc);
        } else if (desc.equals("微信小订单")) {
            recordService.getWxMinOrder((Long) proceedingJoinPoint.getArgs()[0], (String) proceedingJoinPoint.getArgs()[1], desc);
        } else {
            throw new IllegalArgumentException("Unknown user type: " + desc);
        }
    }


    private RecordOpeate getRecordOpeateAnnotation(ProceedingJoinPoint proceedingJoinPoint) {
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        return methodSignature.getMethod().getAnnotation(RecordOpeate.class);
    }
}
